package com.wangchao.administrator.zenmechi.ui.fragment.caipufragment;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.ui.fragment.caipuitemfragment.CaipuItemFragment;
import com.wangchao.administrator.zenmechi.ui.searchactivity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/17.
 */
public class CaipuFragment extends Fragment implements CaipuContract.View,View.OnClickListener{

    @BindView(R.id.tv_guest)
    TextView tvGuest;
    @BindView(R.id.image_search)
    ImageView imageSearch;
    @BindView(R.id.head_layout)
    RelativeLayout headLayout;
    @BindView(R.id.tab_caipu)
    TabLayout tabCaipu;
    @BindView(R.id.vp_caipu)
    ViewPager vpCaipu;
    @BindView(R.id.image_load)
    ImageView imageLoad;
    private List<CaipuItemFragment> fragments = new ArrayList<>();
    private MyAdapterForCaipuViewPager adapter;
    private View view;
    private AnimationDrawable drawable;
    private CaipuPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_caipu, container, false);
        ButterKnife.bind(this, view);

        imageSearch.setOnClickListener(this);
        drawable = (AnimationDrawable) imageLoad.getBackground();
        presenter = new CaipuPresenter(this);
        presenter.getItems();
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.image_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    @Override
    public void initTablayout(List<String> list) {
        for(int i=0;i<list.size();i++){
            tabCaipu.addTab(tabCaipu.newTab().setText(list.get(i)));
        }
    }

    @Override
    public void initFragment(List<String> ids) {
        for (int i=0;i<ids.size();i++){
            CaipuItemFragment fragment = new CaipuItemFragment(ids.get(i));
            fragments.add(fragment);
        }

        adapter = new MyAdapterForCaipuViewPager(getChildFragmentManager(),fragments);
        vpCaipu.setAdapter(adapter);

        vpCaipu.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabCaipu));
        tabCaipu.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vpCaipu.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void startAnim() {
        imageLoad.setVisibility(View.VISIBLE);
        if (!drawable.isRunning()){
            drawable.start();
        }
    }

    @Override
    public void stopAnim() {
        drawable.stop();
        imageLoad.setVisibility(View.GONE);
    }
}
